<template>
  <div>
    <!-- 添加或修改项目验收对话框 -->
    <el-drawer
      ref="drawer"
      :title="title"
      direction="rtl"
      :visible.sync="open"
      size="60%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; min-height: 90vh"
      >
        <el-row>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="有无验收表" prop="required">
                  <el-radio-group disabled v-model="form.required">
                    <el-radio
                      v-for="dict in dict.type.is_required"
                      :key="dict.value"
                      :label="dict.value"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="form.required === '1'">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="项目名称" prop="orderId">
                    <el-select
                      disabled
                      v-model="form.orderId"
                      filterable
                      clearable
                      placeholder="请选择商机"
                      size="small"
                      style="width: 100%"
                    >
                      <el-option
                        v-for="item in orderList"
                        :key="item.orderId"
                        :label="item.orderName"
                        :value="item.orderId"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="客户名称" prop="customerId">
                    <el-select
                      v-model="form.customerId"
                      disabled
                      placeholder="请选择客户名称"
                      style="width: 100%"
                      filterable
                    >
                      <el-option
                        v-for="customer in customerList"
                        :key="customer.customerId"
                        :label="customer.customerName"
                        :value="customer.customerId"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="项目单位" prop="projectUnit">
                    <el-input
                      disabled
                      v-model="form.projectUnit"
                      placeholder="请输入项目单位"
                    >
                      <!-- <template slot="append">{{
                      digitUppercase(form.totalMoney)
                    }}</template> -->
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="验收日期" prop="receptionTime">
                    <el-date-picker
                      disabled
                      clearable
                      style="width: 100%"
                      v-model="form.receptionTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择验收日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="联系人" prop="projectContacts">
                    <el-input
                      disabled
                      v-model="form.projectContacts"
                      placeholder="请输入联系人"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="联系电话" prop="phone">
                    <el-input
                      disabled
                      v-model="form.phone"
                      placeholder="请输入联系电话"
                    >
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="开始时间" prop="totalMoney">
                    <el-date-picker
                      disabled
                      clearable
                      style="width: 100%"
                      v-model="form.startTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择开始时间"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="结束日期" prop="endTime">
                    <el-date-picker
                      disabled
                      clearable
                      style="width: 100%"
                      v-model="form.endTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择结束日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="备注" prop="remark">
                    <el-input
                      disabled
                      v-model="form.remark"
                      type="textarea"
                      placeholder="请输入内容"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="请上传项目验收单" prop="url">
                    <file-upload disabled v-model="form.url" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-divider>审批通道</el-divider>
                <el-form-item label="电子签名" prop="signature">
                  <div class="box">
                    <VueSignaturePad
                      width="100%"
                      height="250px"
                      ref="signaturePad"
                    />
                    <el-button @click="undo" type="text" class="box-button"
                      >清空签名</el-button
                    >
                  </div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="审批意见" prop="message">
                  <el-input
                    v-model="form.message"
                    type="textarea"
                    placeholder="请输入审批意见"
                    :autosize="{ minRows: 4, maxRows: 4 }"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="!disabled">
              <el-col :span="24">
                <el-form-item>
                  <el-button type="primary" @click="handleBtn('PASS')"
                    >通过</el-button
                  >
                  <el-button @click="handleBtn('REJECT')">驳回</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import { getSheet, audit } from "@/api/crm/sheet";

import { getCrmOrderAllNoPage } from "@/api/crm/order";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";

export default {
  name: "Approve",
  dicts: ["is_required"],
  props: {
    /* 业务id */
    value: {
      type: String,
      default: "",
    },

    /* 实例id */
    taskId: {
      type: String,
      default: "",
    },

    /* 是否可以标编辑 */
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 指定跳转节点
      nodeCode: "",
      customerList: [],
      orderList: [],
      rules: {
        // signature: [
        //   {
        //     required: true,
        //     validator: this.validateDetails,
        //     trigger: "change",
        //   },
        // ],
      },
    };
  },
  created() {
    this.reset();
    this.getCrmCustomerAllNoPage();
    this.getCrmOrderAllNoPage();
    this.getSheet();
  },
  mounted(){
    setTimeout(() => {
      let data = window.localStorage.getItem("签名_" + this.$store.getters.userId)
      if(data){
        this.$refs.signaturePad.fromDataURL(data);
      }
    }, 500)
  },
  methods: {
    getCrmOrderAllNoPage() {
      getCrmOrderAllNoPage({}).then((res) => {
        this.orderList = res.data;
      });
    },
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    validateDetails(rule, value, callback) {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
      if (isEmpty) {
        callback(new Error("电子签名必须填写"));
      } else {
        callback();
      }
    },
    // 撤销操作
    undo() {
      this.$refs.signaturePad.clearSignature();
    },
    getCrmCustomerAllNoPage() {
      getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    async getSheet() {
      await getSheet(this.value).then((response) => {
        if (!response.data) {
          this.$modal.alertWarning("代办任务不存在");
        }
        this.form = response.data;
        this.title = this.form.createBy + " 项目验收";
        this.open = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        message: null,
        id: null,
        type: null,
        reason: null,
        startTime: null,
        endTime: null,
        day: null,
        instanceId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 审核通过按钮 */
    handleBtn(skipType) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.taskId = this.taskId;
          this.form.skipType = skipType;
          const { isEmpty, data } = this.$refs.signaturePad.saveSignature();
          this.form.signature = data;
          audit(this.form).then((response) => {
            window.localStorage.setItem("签名_" + this.$store.getters.userId, this.form.signature)
            this.$modal.msgSuccess("办理成功");
            this.open = false;
            this.$emit("refresh");
          });
        }
      });
    },
  },
};
</script>

<style scoped>
::v-deep .el-drawer__header {
  margin-bottom: 20px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .el-textarea.is-disabled .el-textarea__inner {
  background-color: white;
  border-color: #dfe4ed;
  color: black;
  cursor: not-allowed;
}

::v-deep .details .el-table--medium .el-table__cell {
  padding: 0;
}

::v-deep .details .el-input--medium {
  margin: 10px 10px 10px -10px;
}

.box {
  width: 100%;
  height: 250px;
  border: 1px solid #dcdfe6;
  position: relative;
}

.box-button {
  position: absolute;
  top: 215px;
  left: 93%;
}
</style>